package com.gxa.community.agent.service.impl;


import com.gxa.community.agent.service.UserService;
import com.gxa.community.agent.utils.RestUtil;
import com.gxa.community.core.common.CommonVar;
import com.gxa.community.core.common.RequestUrl;
import com.gxa.community.core.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.ObjectUtils;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    //TODO 退出登录
    @Override
    public R loginOut(String token) {
        //从数据库中后台中取出数据
        String url = RequestUrl.ADMIN_URL.getUrl() + "/user/loginOut";
        System.out.println(url);
        MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
        params.add("token", token);
        return RestUtil.postWithParams(url, params);
    }

    //TODO获取用户信息
    @Override
    public R getUserInfo(String token) {
        Object o = redisTemplate.opsForValue().get(CommonVar.AGENT_LOGIN_AXLAB_TOKEN.getKeyWord() + token);
        return !ObjectUtils.isEmpty(o) ? R.ok().put("data", o) : R.error("获取用户信息失败");
    }

}
